package com.mingqijia.gassafety.shared.utils;

import cn.hutool.json.JSONUtil;
import com.gap.basic.base.CommonResponse;
import com.gap.filestorage.service.FileStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;

@Component
@Slf4j
public class FileUtil {

    @Autowired
    FileStorage fileStorage;

    public String uploadFile(String fileName, MultipartFile file) {
        String url = null;
        try {
            byte[] bytes = file.getBytes();
            ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
            CommonResponse commonResponse = fileStorage.uploadPublicFile(fileName, byteStream);
            log.info("发票上传腾讯云响应参数:{}", JSONUtil.toJsonStr(commonResponse));
            url = commonResponse.getData().toString();
            return url;
        } catch (Exception e) {
            log.info("上传腾讯云错误:{}", e.getMessage());
        }
        return url;
    }
}
